package com.example.app.components.MyPlaceSelected

import com.amap.api.location.AMapLocationClient
import com.amap.api.maps.AMap
import com.amap.api.maps.CameraUpdateFactory
import com.amap.api.maps.model.LatLng
import com.amap.api.maps.model.Marker
import com.amap.api.maps.model.MarkerOptions

fun getLatLngHandler(
        props: (
                    (
                        latLng: LatLng,
                        locationClient: AMapLocationClient?,
                        mapInstance: AMap,
                        curMarker: Marker?,
                        title: String,
                ) -> Unit
        ) -> Unit,
        emitsFromGetLatLngHandler: (marker: Marker) -> Unit,
        markerClickHandler: (marker: Marker?) -> Boolean,
) {
    props({ latLng: LatLng, locationClient: AMapLocationClient?, mapInstance: AMap, curMarker: Marker?, title: String ->
        mapInstance.apply {
            setMyLocationEnabled(true)
            //marker点击监听
            setOnMarkerClickListener(object : AMap.OnMarkerClickListener {
                override fun onMarkerClick(p0: Marker?): Boolean {
                    return markerClickHandler(p0)
                }
            })
            //地图移动到当前位置
            moveCamera(
                CameraUpdateFactory.newLatLngZoom(
                    latLng,
                    18f
                )
            )
        }

        curMarker?.remove()
        //添加标记
        var markOption = MarkerOptions()
        markOption.position(latLng)
        markOption.draggable(false)
        markOption.title(title)
        var _curMarker = mapInstance.addMarker(markOption)
        emitsFromGetLatLngHandler(_curMarker)
        //显示地理位置气泡
        _curMarker.showInfoWindow()
        //停止更新位置
        locationClient?.stopLocation()
    })
}